<?php
class api extends ci_controller
{

	function __construct(){
		parent::__construct();

		$this->ci =& get_instance();

		$this->load->library('tank_auth');
	}

	function login(){
		
		$username = $_POST['username'];
		$password = $_POST['password'];

		$get_user_func = 'get_user_by_username';

		if (!is_null($user = $this->ci->users->$get_user_func($username))) {
			$hasher = new PasswordHash(
				$this->config->item('phpass_hash_strength','tank_auth'),
				$this->config->item('phpass_hash_portable','tank_auth')
			);

			$query = $this->db->query("SELECT * FROM users WHERE username='$username' AND id_user_group IN (3,4,5)");

			$row = $query->row();

			if($hasher->CheckPassword($password, $user->password)){
				$hasil = array('status' => 1, 'id_user' => $row->id, 'id_user_group' => $row->id_user_group, 'date_now' => date('d-m-Y H:i'));
			} else {
				$hasil = array('status' => 0);
			}

		} else {
			$hasil = array('status' => 0);

		}

		echo json_encode($hasil);

	}

	function savePerusahaan(){

		$nama_perusahaan = $_POST['nama_perusahaan'];
		$bidang_usaha = $_POST['bidang_usaha'];
		$telepon = $_POST['telepon'];
		$email = $_POST['email'];
		$alamat = $_POST['alamat'];
		$keterangan = $_POST['keterangan'];
		$latitude = $_POST['latitude'];
		$longitude = $_POST['longitude'];

		$data = array('nama_perusahaan' => $nama_perusahaan,
					  'kategori_perusahaan' => $bidang_usaha,
					  'alamat_perusahaan' => $alamat,
					  'longitude_perusahaan' => $longitude,
					  'latitude_perusahaan' => $latitude,
					  'keterangan_perusahaan' => $keterangan,
					  'status_marketing_perusahaan' => 'belum',
					  'id_user_group' => 6
					 );

		$insert = $this->db->insert('perusahaan', $data);

		$id = $this->db->insert_id();
		
		$data_email = array('tipe_kontak' => 'email',
					  'kontak' => $email,
					  'id_global' => $id,
					  'id_user_group' => 6
					 );

		$this->db->insert('kontak', $data_email);

		$data_telepon = array('tipe_kontak' => 'home',
					  'kontak' => $telepon,
					  'id_global' => $id,
					  'id_user_group' => 6
					 );

		$this->db->insert('kontak', $data_telepon);

		if($insert){
			$hasil = array('status' => 1);
		} else {
			$hasil = array('status' => 0);
		}

		echo json_encode($hasil);

	}

}